/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file    stm32h7xx_it.c
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under BSD 3-Clause license,
  * the "License"; You may not use this file except in compliance with the
  * License. You may obtain a copy of the License at:
  *                        opensource.org/licenses/BSD-3-Clause
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32h7xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "scnsInclude.h"
#include "usart.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */

/* USER CODE END TD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
static int32 timer6Us,timer7Us;

int32 __blockSpi4Us;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN PFP */
static void TIM6_CALLBACK(void);
static void TIM7_CALLBACK(void);
/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern DMA_HandleTypeDef hdma_dcmi;
extern DCMI_HandleTypeDef hdcmi;
extern DMA_HandleTypeDef hdma_uart5_rx;
extern DMA_HandleTypeDef hdma_uart5_tx;
extern UART_HandleTypeDef huart5;
extern DMA_HandleTypeDef hdma_spi4_tx;
extern DMA_HandleTypeDef hdma_spi4_rx;
extern SPI_HandleTypeDef hspi4;
/* USER CODE BEGIN EV */
void stm32h7xx_it_init(void)
{
    scnsMenuAddConst(0,__blockSpi4Us,,SCNS_TYPE_INT32);
    scnsMenuAddConst(0,timer6Us,,SCNS_TYPE_INT32);
    scnsMenuAddConst(0,timer7Us,,SCNS_TYPE_INT32);
}
/* USER CODE END EV */

/******************************************************************************/
/*           Cortex Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */

  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Memory management fault.
  */
void MemManage_Handler(void)
{
  /* USER CODE BEGIN MemoryManagement_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END MemoryManagement_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */
    /* USER CODE END W1_MemoryManagement_IRQn 0 */
  }
}

/**
  * @brief This function handles Pre-fetch fault, memory access fault.
  */
void BusFault_Handler(void)
{
  /* USER CODE BEGIN BusFault_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END BusFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_BusFault_IRQn 0 */
    /* USER CODE END W1_BusFault_IRQn 0 */
  }
}

/**
  * @brief This function handles Undefined instruction or illegal state.
  */
void UsageFault_Handler(void)
{
  /* USER CODE BEGIN UsageFault_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END UsageFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_UsageFault_IRQn 0 */
    /* USER CODE END W1_UsageFault_IRQn 0 */
  }
}

/**
  * @brief This function handles System service call via SWI instruction.
  */
void SVC_Handler(void)
{
  /* USER CODE BEGIN SVCall_IRQn 0 */
    scnsYingErrorHandler(0,0,__FILE__,__LINE__,__FUNCTION__);
  /* USER CODE END SVCall_IRQn 0 */
  /* USER CODE BEGIN SVCall_IRQn 1 */

  /* USER CODE END SVCall_IRQn 1 */
}

/**
  * @brief This function handles Debug monitor.
  */
void DebugMon_Handler(void)
{
  /* USER CODE BEGIN DebugMonitor_IRQn 0 */
  /* USER CODE END DebugMonitor_IRQn 0 */
  /* USER CODE BEGIN DebugMonitor_IRQn 1 */

  /* USER CODE END DebugMonitor_IRQn 1 */
}

/**
  * @brief This function handles Pendable request for system service.
  */
void PendSV_Handler(void)
{
  /* USER CODE BEGIN PendSV_IRQn 0 */

  /* USER CODE END PendSV_IRQn 0 */
  /* USER CODE BEGIN PendSV_IRQn 1 */

  /* USER CODE END PendSV_IRQn 1 */
}

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */
    scnsTimerSysTickHandler();

  /* USER CODE END SysTick_IRQn 1 */
}

/******************************************************************************/
/* STM32H7xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32h7xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles DMA1 stream0 global interrupt.
  */
void DMA1_Stream0_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream0_IRQn 0 */

  /* USER CODE END DMA1_Stream0_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_spi4_tx);
  /* USER CODE BEGIN DMA1_Stream0_IRQn 1 */

  /* USER CODE END DMA1_Stream0_IRQn 1 */
}

/**
  * @brief This function handles DMA1 stream1 global interrupt.
  */
void DMA1_Stream1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream1_IRQn 0 */

  /* USER CODE END DMA1_Stream1_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_dcmi);
  /* USER CODE BEGIN DMA1_Stream1_IRQn 1 */

  /* USER CODE END DMA1_Stream1_IRQn 1 */
}

/**
  * @brief This function handles DMA1 stream2 global interrupt.
  */
void DMA1_Stream2_IRQHandler(void)
{
  /* USER CODE BEGIN DMA1_Stream2_IRQn 0 */

  /* USER CODE END DMA1_Stream2_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_spi4_rx);
  /* USER CODE BEGIN DMA1_Stream2_IRQn 1 */

  /* USER CODE END DMA1_Stream2_IRQn 1 */
}

/**
  * @brief This function handles UART5 global interrupt.
  */
void UART5_IRQHandler(void)
{
  /* USER CODE BEGIN UART5_IRQn 0 */

  /* USER CODE END UART5_IRQn 0 */
  HAL_UART_IRQHandler(&huart5);
  /* USER CODE BEGIN UART5_IRQn 1 */

  /* USER CODE END UART5_IRQn 1 */
}

/**
  * @brief This function handles TIM6 global interrupt, DAC1_CH1 and DAC1_CH2 underrun error interrupts.
  */
void TIM6_DAC_IRQHandler(void)
{
  /* USER CODE BEGIN TIM6_DAC_IRQn 0 */
    if(LL_TIM_IsActiveFlag_UPDATE(TIM6)==SET)
    {
        LL_TIM_ClearFlag_UPDATE(TIM6);
        TIM6_CALLBACK();
    }
  /* USER CODE END TIM6_DAC_IRQn 0 */

  /* USER CODE BEGIN TIM6_DAC_IRQn 1 */

  /* USER CODE END TIM6_DAC_IRQn 1 */
}

/**
  * @brief This function handles TIM7 global interrupt.
  */
void TIM7_IRQHandler(void)
{
  /* USER CODE BEGIN TIM7_IRQn 0 */
    if(LL_TIM_IsActiveFlag_UPDATE(TIM7)==SET)
    {
        LL_TIM_ClearFlag_UPDATE(TIM7);
        TIM7_CALLBACK();
    }
  /* USER CODE END TIM7_IRQn 0 */
  /* USER CODE BEGIN TIM7_IRQn 1 */

  /* USER CODE END TIM7_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream0 global interrupt.
  */
void DMA2_Stream0_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream0_IRQn 0 */

  /* USER CODE END DMA2_Stream0_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_uart5_rx);
  /* USER CODE BEGIN DMA2_Stream0_IRQn 1 */

  /* USER CODE END DMA2_Stream0_IRQn 1 */
}

/**
  * @brief This function handles DMA2 stream1 global interrupt.
  */
void DMA2_Stream1_IRQHandler(void)
{
  /* USER CODE BEGIN DMA2_Stream1_IRQn 0 */

  /* USER CODE END DMA2_Stream1_IRQn 0 */
  HAL_DMA_IRQHandler(&hdma_uart5_tx);
  /* USER CODE BEGIN DMA2_Stream1_IRQn 1 */

  /* USER CODE END DMA2_Stream1_IRQn 1 */
}

/**
  * @brief This function handles DCMI global interrupt.
  */
void DCMI_IRQHandler(void)
{
  /* USER CODE BEGIN DCMI_IRQn 0 */
  /* USER CODE END DCMI_IRQn 0 */
  HAL_DCMI_IRQHandler(&hdcmi);
  /* USER CODE BEGIN DCMI_IRQn 1 */

  /* USER CODE END DCMI_IRQn 1 */
}

/**
  * @brief This function handles SPI4 global interrupt.
  */
void SPI4_IRQHandler(void)
{
  /* USER CODE BEGIN SPI4_IRQn 0 */

  /* USER CODE END SPI4_IRQn 0 */
  HAL_SPI_IRQHandler(&hspi4);
  /* USER CODE BEGIN SPI4_IRQn 1 */

  /* USER CODE END SPI4_IRQn 1 */
}

/**
  * @brief This function handles BDMA channel0 global interrupt.
  */
void BDMA_Channel0_IRQHandler(void)
{
  /* USER CODE BEGIN BDMA_Channel0_IRQn 0 */
    if(LL_BDMA_IsActiveFlag_TC0(BDMA))
    {
        LL_BDMA_ClearFlag_TC0(BDMA);
        LL_BDMA_DisableChannel(BDMA,LL_BDMA_CHANNEL_0);
        scnsSmctpSendFinishCallback();
    }
    if(LL_BDMA_IsActiveFlag_TE0(BDMA))
    {
        LL_BDMA_ClearFlag_TE0(BDMA);
        LL_BDMA_DisableChannel(BDMA,LL_BDMA_CHANNEL_0);
        hxPrintfFL("LL_BDMA_IsActiveFlag_TE0");
        scnsSmctpSendFinishCallback();
    }
  /* USER CODE END BDMA_Channel0_IRQn 0 */

  /* USER CODE BEGIN BDMA_Channel0_IRQn 1 */

  /* USER CODE END BDMA_Channel0_IRQn 1 */
}

/**
  * @brief This function handles BDMA channel1 global interrupt.
  */
void BDMA_Channel1_IRQHandler(void)
{
  /* USER CODE BEGIN BDMA_Channel1_IRQn 0 */
    if(LL_BDMA_IsActiveFlag_TC1(BDMA))
    {
        LL_BDMA_ClearFlag_TC1(BDMA);
        scnsSmctpReceive(NULL,0,0);
    }
    if(LL_BDMA_IsActiveFlag_HT1(BDMA))
    {
        LL_BDMA_ClearFlag_HT1(BDMA);
        scnsSmctpReceive(NULL,0,1);
    }
    if(LL_BDMA_IsActiveFlag_TE1(BDMA))
    {
        LL_BDMA_ClearFlag_TE1(BDMA);
        hxPrintfFL("LL_BDMA_IsActiveFlag_TE1");
    }
  /* USER CODE END BDMA_Channel1_IRQn 0 */

  /* USER CODE BEGIN BDMA_Channel1_IRQn 1 */

  /* USER CODE END BDMA_Channel1_IRQn 1 */
}

/**
  * @brief This function handles LPUART1 global interrupt.
  */
void LPUART1_IRQHandler(void)
{
  /* USER CODE BEGIN LPUART1_IRQn 0 */
    if(LL_LPUART_IsActiveFlag_IDLE(LPUART1))
    {
        LL_LPUART_ClearFlag_IDLE(LPUART1);
        scnsSmctpReceive(NULL,0,2);
    }
  /* USER CODE END LPUART1_IRQn 0 */
  /* USER CODE BEGIN LPUART1_IRQn 1 */

  /* USER CODE END LPUART1_IRQn 1 */
}

/* USER CODE BEGIN 1 */

static void TIM6_CALLBACK(void)
{
    volatile uint64 timeUs=scnsTimerGetUs();
#if defined(SCNS_LED_ENABLE)&&SCNS_LED_ENABLE==1
    static const ScnsLedEnum group[]={
        SCNS_LED_E3,
        SCNS_LED_PCF3,
        SCNS_LED_D15,
        SCNS_LED_STB0_D16,
    };
    scnsLedGroupFlow(group,(sizeof(group))/(sizeof(group[0])),250000);
#endif
    if(scnsTimerGetInitedTick()!=0)
    {
#if defined(SCNS_TM1638_ENABLE)&&SCNS_TM1638_ENABLE==1
        scnsTm1638Sync(SCNS_TM1638_ON_BOARD);
        scnsTm1638Sync(SCNS_TM1638_STB0);
#endif
#if defined(SCNS_KEYBOARD_GPIO_ENABLE)&&SCNS_KEYBOARD_GPIO_ENABLE==1
        scnsKeyboardGpioInterrupt();
#endif
#if defined(SCNS_GT911_ENABLE)&&SCNS_GT911_ENABLE==1
        scnsGt911Interrupt(SCNS_GT911_1);
#endif
#if defined(SCNS_BH1750_ENABLE)&&SCNS_BH1750_ENABLE==1
        scnsBh1750Interrupt(SCNS_BH1750_1);
#endif
#if defined(SCNS_TOUCH_SCREEN_ENABLE)&&SCNS_TOUCH_SCREEN_ENABLE==1
        scnsTouchScreenScan();
#endif
#if defined(SCNS_DS18B20_ENABLE)&&SCNS_DS18B20_ENABLE==1
        scnsDs18b20Interrupt(SCNS_DS18B20_1);
#endif
#if defined(SCNS_DHT11_ENABLE)&&SCNS_DHT11_ENABLE==1
        scnsDht11Interrupt(SCNS_DHT11_1);
#endif
#if defined(SCNS_HX711_ENABLE)&&SCNS_HX711_ENABLE==1
        scnsHx711Interrupt(SCNS_HX711_1);
#endif
#if defined(SCNS_KEY_ENABLE)&&SCNS_KEY_ENABLE==1
        scnsKeyCallback();
#endif
    }
    timer6Us=scnsTimerGetUs()-timeUs;
    if(scnsMenuCanPageSend(0))
    {
        scnsSmctpExtOscilloscopeSendFloat(SCNS_SMCTP_CHANNEL_VISUAL_SCOPE,0,timer6Us,timer7Us,__blockSpi4Us,scnsRandBetween(0,100)/*,uart8Us,testUs,exti95Us,dma14Us,dma25Us*/);
        __blockSpi4Us=0;
    }
}

static void TIM7_CALLBACK(void)
{
    volatile uint64 timeUs=scnsTimerGetUs();
#if defined(SCNS_BEEP_ENABLE)&&SCNS_BEEP_ENABLE==1
    scnsBeepInterrupt();
#endif
#if defined(SCNS_PCF8574_ENABLE)&&SCNS_PCF8574_ENABLE==1
    scnsPcf8574Sync(SCNS_PCF8574_1);
#endif
    if(scnsTimerGetInitedTick()!=0)
    {
#if defined(SCNS_GYRO_ENABLE)&&SCNS_GYRO_ENABLE==1
        scnsGyroInterrupt(SCNS_GYRO_1);
#endif
    }
#if defined(SCNS_SMCTP_ENABLE)&&SCNS_SMCTP_ENABLE==1
    scnsSmctpFlushBuf();
#endif
    timer7Us=scnsTimerGetUs()-timeUs;
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef*huart)
{
    if(huart==&huart5)
    {
#if defined(SCNS_ESP_AT_UART_ENABLE)&&SCNS_ESP_AT_UART_ENABLE==1
        scnsEspAtUartSendFinishCallback(SCNS_ESP_AT_UART_1);
#endif
#if defined(SCNS_CH9141_AT_UART_ENABLE)&&SCNS_CH9141_AT_UART_ENABLE==1
        scnsCh9141AtUartSendFinishCallback(SCNS_CH9141_AT_UART_1);
#endif
    }
}

void HAL_UART_ErrorCallback(UART_HandleTypeDef*huart)
{
    if(huart==&huart5)
    {
#if defined(SCNS_ESP_AT_UART_ENABLE)&&SCNS_ESP_AT_UART_ENABLE==1
        scnsEspAtUartErrorCallback(SCNS_ESP_AT_UART_1);
#endif
#if defined(SCNS_CH9141_AT_UART_ENABLE)&&SCNS_CH9141_AT_UART_ENABLE==1
        scnsCh9141AtUartSendFinishCallback(SCNS_CH9141_AT_UART_1);
#endif
    }
}

void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef*huart,uint16_t Size)
{
    if(huart==&huart5)
    {
#if defined(SCNS_ESP_AT_UART_ENABLE)&&SCNS_ESP_AT_UART_ENABLE==1
        scnsEspAtUartReceive(SCNS_ESP_AT_UART_1,NULL,Size);
#endif
#if defined(SCNS_CH9141_AT_UART_ENABLE)&&SCNS_CH9141_AT_UART_ENABLE==1
        scnsCh9141AtUartReceive(SCNS_CH9141_AT_UART_1,NULL,Size);
#endif
    }
}

void HAL_DCMI_FrameEventCallback(DCMI_HandleTypeDef*hdcmi)
{
#if defined(SCNS_OV7725_ENABLE)&&SCNS_OV7725_ENABLE==1
    scnsOv7725DmaFinishInterrupt(SCNS_OV7725_1);
#endif
#if defined(SCNS_OV2640_ENABLE)&&SCNS_OV2640_ENABLE==1
    scnsOv2640DmaFinishInterrupt(SCNS_OV2640_1);
#endif
#if defined(SCNS_OV5640_ENABLE)&&SCNS_OV5640_ENABLE==1
    scnsOv5640DmaFinishInterrupt(SCNS_OV5640_1);
#endif
}

void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef*hspi)
{
    if(&hspi4==hspi)
    {
#if defined(SCNS_ST7735_ENABLE)&&SCNS_ST7735_ENABLE==1
        if(!(LCD_CS1_GPIO_Port->IDR&LCD_CS1_Pin))
        {
            scnsSt7735AsyncCallback(SCNS_ST7735_1);
        }
#endif
#if defined(SCNS_ST7796_ENABLE)&&SCNS_ST7796_ENABLE==1
        if(!(LCD_CS2_GPIO_Port->IDR&LCD_CS2_Pin))
        {
            scnsSt7796AsyncCallback(SCNS_ST7796_1);
        }
#endif
#if defined(SCNS_ST7789_ENABLE)&&SCNS_ST7789_ENABLE==1
        if(!(LCD_CS2_GPIO_Port->IDR&LCD_CS2_Pin))
            scnsSt7789AsyncCallback(SCNS_ST7789_1);
#endif
#if defined(SCNS_ILI9341_ENABLE)&&SCNS_ILI9341_ENABLE==1
        if(!(LCD_CS2_GPIO_Port->IDR&LCD_CS2_Pin))
            scnsIli9341AsyncCallback(SCNS_ILI9341_1);
#endif
    }
}

void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef*hspi)
{
    if(&hspi4==hspi)
    {
#if defined(SCNS_ST7735_ENABLE)&&SCNS_ST7735_ENABLE==1
        if(!(LCD_CS1_GPIO_Port->IDR&LCD_CS1_Pin))
        {
            scnsSt7735AsyncCallback(SCNS_ST7735_1);
        }
#endif
#if defined(SCNS_ST7796_ENABLE)&&SCNS_ST7796_ENABLE==1
        if(!(LCD_CS2_GPIO_Port->IDR&LCD_CS2_Pin))
        {
            scnsSt7796AsyncCallback(SCNS_ST7796_1);
        }
#endif
#if defined(SCNS_ST7789_ENABLE)&&SCNS_ST7789_ENABLE==1
        if(!(LCD_CS2_GPIO_Port->IDR&LCD_CS2_Pin))
            scnsSt7789AsyncCallback(SCNS_ST7789_1);
#endif
#if defined(SCNS_ILI9341_ENABLE)&&SCNS_ILI9341_ENABLE==1
        if(!(LCD_CS2_GPIO_Port->IDR&LCD_CS2_Pin))
            scnsIli9341AsyncCallback(SCNS_ILI9341_1);
#endif
    }
}
/* USER CODE END 1 */
